/****************************************************************************
 * @file    DataParser.h
 * @brief   Data parsing utilities using multiple unions and byte parameters.
 * @details This module provides functions to parse data from high and low
 *          bytes into various data types such as uint16_t, int16_t, int32_t,
 *          and float using multiple unions.
 *
 * @author  YourName
 * @date    YYYY-MM-DD
 ****************************************************************************/

#ifndef DATAPARSER_H
#define DATAPARSER_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

// Union for interpreting 2 bytes as uint16_t or int16_t
typedef union {
    uint8_t bytes[2];
    uint16_t u16;
    int16_t i16;
} U16Union;

// Union for interpreting 4 bytes as uint32_t, int32_t, or float
typedef union {
    uint8_t bytes[4];
    uint32_t u32;
    uint16_t u16[2];
    int32_t i32;
    float f32;
} U32Union;

// Functions to parse data from high and low bytes
uint16_t parse_uint16(uint8_t highByte, uint8_t lowByte);
int16_t parse_int16(uint8_t highByte, uint8_t lowByte);
uint32_t parse_uint32(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3);
int32_t parse_int32(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3);
float parse_float(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3);


// Functions to convert parsed data back into bytes
void uint16_to_bytes(uint16_t value, uint8_t *highByte, uint8_t *lowByte);
void int16_to_bytes(int16_t value, uint8_t *highByte, uint8_t *lowByte);
void uint32_to_bytes(uint32_t value, uint8_t *byte0, uint8_t *byte1, uint8_t *byte2, uint8_t *byte3);
void int32_to_bytes(int32_t value, uint8_t *byte0, uint8_t *byte1, uint8_t *byte2, uint8_t *byte3);
void float_to_bytes(float value, uint8_t *byte0, uint8_t *byte1, uint8_t *byte2, uint8_t *byte3);



// Macros to parse data from high and low bytes
#define PARSE_UINT16(highByte, lowByte)  ((uint16_t)(((uint16_t)(highByte) << 8) | (uint16_t)(lowByte)))
#define PARSE_INT16(highByte, lowByte)   ((int16_t)(((int16_t)(highByte) << 8) | (int16_t)(lowByte)))
#define PARSE_UINT32(byte0, byte1, byte2, byte3) \
    ((uint32_t)(((uint32_t)(byte0) << 24) | ((uint32_t)(byte1) << 16) | ((uint32_t)(byte2) << 8) | (uint32_t)(byte3)))
#define PARSE_INT32(byte0, byte1, byte2, byte3) \
    ((int32_t)(((int32_t)(byte0) << 24) | ((int32_t)(byte1) << 16) | ((int32_t)(byte2) << 8) | (int32_t)(byte3)))
#define PARSE_FLOAT(byte0, byte1, byte2, byte3) \
    (*((float*)&((uint32_t)(((uint32_t)(byte0) << 24) | ((uint32_t)(byte1) << 16) | ((uint32_t)(byte2) << 8) | (uint32_t)(byte3)))))

#define MAX_PACKET_SIZE 264 // 包含帧头、功能码、长度、Payload 和 CRC 的最大包大小

typedef enum {
    STATE_WAIT_HEADER1,  // 等待帧头 0xAA
    STATE_WAIT_HEADER2,  // 等待帧头 0x55
    STATE_RECEIVE_REMAINING  // 接收剩余数据
} ReceiveState;

typedef struct {
    ReceiveState state;      // 当前状态
    uint8_t buffer[MAX_PACKET_SIZE];  // 数据缓存
    uint16_t index;           // 当前接收索引
    uint16_t expected_length; // 预期的数据包长度
} ProtocolParser;
void protocol_parser_init(ProtocolParser *parser);
uint8_t protocol_parser_receive_byte(ProtocolParser *parser, uint8_t byte);
uint8_t* get_payload_data(ProtocolParser *parser);
uint16_t get_payload_length(ProtocolParser *parser);
#ifdef __cplusplus
}
#endif

#endif // DATAPARSER_H
